package com.bysc.cmsservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.commonutils.vo.CmsActivityQueryVo;
import com.bysc.cmsservice.service.CmsActivityService;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.enums.ActivityEnum;
import com.bysc.commonutils.utils.EnumUtils;
import com.bysc.commonutils.vo.CmsActivity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-10-24
 */
@RestController
@RequestMapping("/cmsservice/cms-activity")
@Api(tags = "活动管理")
public class CmsActivityController {

    @Autowired
    private CmsActivityService cmsActivityService;

    @ApiOperation("新增活动")
    @PostMapping("addActivity")
    public Results addActivity(@RequestBody CmsActivity cmsActivity, HttpServletRequest request) {
        String clubId =  JwtUtils.getClubIdByJwtToken(request);
        cmsActivity.setClubId(clubId);
        cmsActivityService.save(cmsActivity);
        return Results.ok().data("activity", cmsActivity).message(MessageConstant.ADD_ACTIVITY_SUCCESS);
    }


    @ApiOperation("根据id查找活动")
    @GetMapping("queryActivityById/{id}")
    public Results queryActivityById(@PathVariable String id) {
        CmsActivity activity = cmsActivityService.getById(id);
        return Results.ok().data("activity", activity).message(MessageConstant.QUERY_ACTIVITY_SUCCESS);
    }

    @ApiOperation("更新活动（后台 传id修改）")
    @PostMapping("updateActivity")
    public Results updateActivity(@RequestBody CmsActivity cmsActivity) {
        cmsActivityService.updateById(cmsActivity);
        return Results.ok().message(MessageConstant.UPDATE_ACTIVITY_SUCCESS);
    }

    /**
     * 获取所有活动信息

     * @return
     */
    @ApiOperation("查询所有活动")
    @PostMapping("getAllActivityList")
    public Results getAllActivityList(
    ) {
        List<CmsActivity> list = cmsActivityService.list(null);
        return Results.ok().data("list", list).message(MessageConstant.QUERY_ACTIVITY_SUCCESS);
    }


    /*
    * 新增根据clueId查询活动
    * 已经失效的活动会查询不到
    * （前后台通用）
    * */
    @ApiOperation("分页查询活动（前后台通用）")
    @PostMapping("getActivityList/{page}/{limit}")
    public Results getActivityList(@ApiParam(name = "page", value = "查询页数", required = true) @PathVariable Integer page,
                                   @ApiParam(name = "limit", value = "查询数量", required = true) @PathVariable Integer limit,
                                   @RequestBody(required = false) CmsActivityQueryVo queryVo,HttpServletRequest request
    ) {
        Page<CmsActivity> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<CmsActivity> wrapper = new LambdaQueryWrapper<>();

        if (queryVo != null) {
            String id = queryVo.getId();
            String clubId = queryVo.getClubId();
            String value = queryVo.getValue();
            String address = queryVo.getAddress();
            Date joinStart = queryVo.getJoinStart();
            Date joinEnd = queryVo.getJoinEnd();
            Date activityStart = queryVo.getActivityStart();
            Date activityEnd = queryVo.getActivityEnd();
            Integer status = queryVo.getStatus();
            if (!StringUtils.isEmpty(clubId)) {
                wrapper.eq(CmsActivity::getClubId, clubId);
            }
            if (!StringUtils.isEmpty(id)) {
                wrapper.eq(CmsActivity::getId, id);
            }
            if (!StringUtils.isEmpty(value)) {
                wrapper.like(CmsActivity::getRules, value).or().like(CmsActivity::getSubject, value);
            }
            if (!StringUtils.isEmpty(address)) {
                wrapper.like(CmsActivity::getAddress, address);
            }
            if (joinStart != null && joinEnd != null) {
                wrapper.between(CmsActivity::getJoinStart, joinStart, joinEnd);
            }
            if (activityStart != null && activityEnd != null) {
                wrapper.between(CmsActivity::getActivityStart, activityStart, activityEnd);
            }
            if (status != null) {
                wrapper.eq(CmsActivity::getStatus, status);
            }

        }
        String TOKEN_clubId = JwtUtils.getClubIdByJwtToken(request);
        if(!StringUtils.isEmpty(TOKEN_clubId)){
            wrapper.eq(CmsActivity::getClubId, TOKEN_clubId);
        }
        wrapper.orderByDesc(CmsActivity::getGmtCreate);
        IPage<CmsActivity> cmsActivityIPage = cmsActivityService.page(pageParam, wrapper);

        for (CmsActivity record : cmsActivityIPage.getRecords()) {
            if (record != null && record.getActivityStart() != null)
                record.setStatusName(ActivityEnum.getValueById(record.getStatus()));
        }
        return Results.ok().data("list", cmsActivityIPage).message(MessageConstant.QUERY_ACTIVITY_SUCCESS);
    }


    @ApiOperation("根据id删除活动")
    @DeleteMapping("deleteActivityById/{id}")
    public Results deleteActivityById(@PathVariable String id) {
        cmsActivityService.removeById(id);
        return Results.ok().message(MessageConstant.DELETE_ACTIVITY_SUCCESS);
    }

    @ApiOperation("获取活动状态信息")
    @GetMapping("getStatus")
    public Results getStatus() {
        List<Map<String, Object>> map = EnumUtils.enumToListMap(ActivityEnum.class);
        return Results.ok().data("activityEnum", map).message(MessageConstant.QUERY_ACTIVITY_SUCCESS);
    }
}

